package com.sias.spring.mybatis;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author 吴文杰
 * @version 1.0
 * @createTime 2023-05-30 14:26:47
 */
public class SograFactoryBean implements FactoryBean {

  private SqlSession sqlSession;

  private Class mapperClass;

  public SograFactoryBean(Class mapperClass) {
    this.mapperClass = mapperClass;
  }

  @Autowired
  public void setSqlSession(SqlSessionFactory sqlSessionFactory) {
    sqlSessionFactory.getConfiguration().addMapper(mapperClass);
    this.sqlSession = sqlSessionFactory.openSession();
  }

  @Override
  public Object getObject() throws Exception {
    // return Proxy.newProxyInstance(SograFactoryBean.class.getClassLoader(), new Class[]{UserMapper.class}, new InvocationHandler() {
    //   @Override
    //   public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    //     System.out.println(method.getName());
    //     return null;
    //   }
    // });

    return sqlSession.getMapper(mapperClass);
  }

  @Override
  public Class<?> getObjectType() {
    return mapperClass;
  }
}
